
/*
脚本说明:存储日程事件(添加，修改，删除)。
程序员:
*/

//脚本唯一关键字,通过该关键字找到对应脚本，每个脚本必须有该描述。
var v_script_id = "dc_event_editor";
//脚本状态,0:该脚本不许执行，1:该脚本可以执行
var v_status = "1";

//脚本逻辑写在run语句之间
if (run) {
	action.vars.put(action.V_OUTPUT_FILE_FORMAT, "2");//输出xml。
	/*
	<?xml version='1.0' ?><data><action type='inserted' sid='1293769246512' tid='87' ></action></data>
	*/
	action.vars.put(action.HANDY_DYNA_FTL, "com/handy/plugin/dayscheduler/files/ftl_server/editor_result.ftl");//指定数据输出模板
	/*
	1293769246512_!nativeeditor_status	inserted/updated/deleted       
	1293769246512_end_date	2009-11-01 07:55
	1293769246512_id	1293769246512
	1293769246512_start_date	2009-11-01 05:00
	1293769246512_text	aaaaaaaaaaaaaaaaaaaaaaaaaaa
	ids	1293769246512
	*/
	var ids = action.form.getString("ids");
	if (ids != null) {
		var eventId = action.form.getString(ids + "_id");
		var nativeEditorStatus = action.form.getString(ids + "_!nativeeditor_status");
		var text = action.form.getString(ids + "_text");
		var startDate = action.form.getString(ids + "_start_date");
		var endDate = action.form.getString(ids + "_end_date");
		var sql = "";
		var userid = action.getSession().getAttribute("user").id;
		//println("nativeEditorStatus="+nativeEditorStatus);
		if (nativeEditorStatus == "inserted") {
			sql = "insert into h_dc_events (userid,event_id,start_date,end_date,details)values(" + userid + "," + eventId + ",to_date('" + startDate + "','yyyy-mm-dd hh24:mi:ss'),to_date('" + endDate + "','yyyy-mm-dd hh24:mi:ss'),'" + text + "')";
		} else if (nativeEditorStatus == "updated") {
			sql = "update h_dc_events set start_date=to_date('" + startDate + "','yyyy-mm-dd hh24:mi:ss'),end_date=to_date('" + endDate + "','yyyy-mm-dd hh24:mi:ss'),details='"+text+"' where userid=" + userid + " and event_id=" + eventId;
		} else if (nativeEditorStatus == "deleted") {
			sql = "delete from h_dc_events where userid=" + userid + " and event_id=" + eventId;
		}
		//println(sql);
		//执行sql
		if (action.dbUtil.update("", sql) == 0) {
			action.errorCode = 2;//数据库操作失败
			action.retString = '数据库操作失败   ' ;//错误文字说明
		}		

		//相关的变量传给ftl
		action.root.put("nativeEditorStatus", nativeEditorStatus);
		action.root.put("ids", ids);
	}
	
	//返回执行结果,如：
	//<?xml version='1.0' ?><data><action type='deleted' sid='89' tid='89' ></action></data>
}

